/*
 * @(#) $Header$
 *
 * Copyright (C)  2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.logging;

import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * Class {@code JTextAreaHandler} is a {@link Handler} that sinks its records
 * into a {@link JTextArea}. This class is strongly inspired from
 * <a href="http://textareaappender.zcage.com/">ZCage</a>.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.logging.JTextAreaHandler">Daniel Léonard</a>
 * @version $Revision$
 */
public class JTextAreaHandler extends Handler {

//---------------------------
// Instance variables
//---------------------------

   /** The text area in which to write each log record. */
   private JTextArea sink;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   sink   the text area.
    */
   public JTextAreaHandler(JTextArea sink) {
      this.setSink(sink);
      this.setFormatter(new SimpleFormatter());
      }


//---------------------------
// Constructors
//---------------------------

   /**
    * Changes the text area.
    * @param   sink   the new text area.
    */
   public void setSink(JTextArea sink) {
      this.sink = sink;
      }

   /**
    * Gets the text area.
    * @return   the text area.
    */
   public JTextArea getSink() {
      return this.sink;
      }


//---------------------------
// Implemented methods from java.util.logging.Handler
//---------------------------

   /**
    * Publishes one log record.
    * @param   record   the log record.
    */
   @Override
   @SuppressWarnings("hiding")
   public void publish(LogRecord record) {
      final JTextArea sink = this.getSink();

   // no need to publish if the text area is not present
      boolean should_log = (null != sink) && this.isLoggable(record);

      if (should_log) {
         Formatter formatter = this.getFormatter();
         final String message = formatter.format(record);
         SwingUtilities.invokeLater(new Runnable() {
            @Override
            @SuppressWarnings("null")
            public void run() {
               sink.append(message);

               String text = sink.getText();
               int len = text.length();
               sink.setCaretPosition(len);
               }
            });
         }
      }

   /**
    * Does nothing.
    */
   @Override
   public void close() throws SecurityException {
   // nothing
      }

   /**
    * Does nothing.
    */
   @Override
   public void flush() {
   // nothing
      }

   }
